package com.lucy.wx.miniapp.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@Slf4j
@Aspect
@Component
public class WxMiniGrantAspect {
    /**
     * 以自定义注解作为切入
     */
    @Pointcut("@annotation(com.lucy.wx.miniapp.anno.WxMiniGrant)")
    public void WxMiniGrant(){}

    @Around("WxMiniGrant()")
    public Object process(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取请求IP
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        Enumeration<String> e =  request.getHeaders("Referer");//获取多个同名请求头对应的一组value值，因此返回枚举类型数据
        while(e.hasMoreElements()){
            //遍历枚举中存储的每一个元素
            String value = e.nextElement();
            System.out.println(value);//将值输出
        }
        // 会话参数治理
        HttpSession session = request.getSession();
        //第二步：将想要保存到数据存入session中
        session.setAttribute("appId","testApp");
        Object object = joinPoint.proceed();
        return object;
    }
}
